//
//  CityTableViewController.m
//  Weather-Test
//
//  Created by My Mac on 2017/4/30.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "CityTableViewController.h"
#import "CityGroup.h"
#import "SearchViewController.h"
@interface CityTableViewController ()

//总城市列表
@property(nonatomic,strong) NSArray *cityArray;

@end

@implementation CityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化控件
    [self initViews];
    //懒加载
    [self cityArray];
    
}

//懒加载
-(NSArray *)cityArray
{
    if (!_cityArray) {
        //拿到plist文件路径
        NSString *path = [[NSBundle mainBundle]pathForResource:@"cityGroups.plist" ofType:nil];
        //根据路径将plist文件数据保存到数组
        NSArray *cityGroupArr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *cityMarr = [NSMutableArray new];
        //字典转模型 使用for in 循环
        for (NSDictionary *dict in cityGroupArr) {
            CityGroup *cityGroup = [CityGroup cityGroupWithDict:dict];
            [cityMarr addObject:cityGroup];
        }
        _cityArray = cityMarr;
    }
    return _cityArray;
}

-(void)initViews
{
    self.navigationItem.title = @"城市列表";
    
    UIBarButtonItem *backItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_nomorl"] style:UIBarButtonItemStyleDone target:self action:@selector(BackItem)];
    self.navigationItem.leftBarButtonItem = backItem;
    
    UIBarButtonItem *seachItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchCity)];
    self.navigationItem.rightBarButtonItem = seachItem;
}

-(void)BackItem
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)searchCity
{
    SearchViewController *searchVC = [[SearchViewController alloc]init];
    searchVC.navigationItem.title = @"搜索城市";
    NSMutableArray *marr = [NSMutableArray array];
    for (int i=1; i<_cityArray.count; i++) {
        for (int j=0; j<[[_cityArray[i] cities] count]; j++) {
            NSString *cityName = [_cityArray[i] cities][j];
            [marr addObject:cityName];
        }
    }
    searchVC.cityNameArr = marr;
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _cityArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[_cityArray[section] cities] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == NULL) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = [_cityArray[indexPath.section] cities][indexPath.row];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [_cityArray[section] title];
}

//索引条
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [_cityArray valueForKeyPath:@"title"];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54.f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cityName = [_cityArray[indexPath.section] cities][indexPath.row];
    if (_getCityName != nil) {
        _getCityName(cityName);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
